/****************************************************************************
*                                                                           *
* Copyright (c) RSC. All rights reserved(2010).								*
* Developer: Marius Ionescu    						                        *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.Security;
using System.Web.UI.WebControls;
using NS_Controls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.UI;
using AjaxControlToolkit;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;


public partial class NomenclatorSIUI : SessionPageBase
{
    private string connectionString_XML = "Data Source=DEVH2;Initial Catalog=SIUI_XML;User Id=sa;Password=sa;";
    private string stringTempID = string.Empty;
    private string connectionString
    {
        get { return (string)(this.ViewState["connectionString"] ?? ""); }
        set { this.ViewState["connectionString"] = value; }
    }

    public string[] HiddenColumns
    {
        get { return (string[])(this.Page.Session[this.ClientID + "_HiddenColumns"] ?? new string[] { }); }
        set { this.Page.Session[this.ClientID + "_HiddenColumns"] = value; }
    }

    private int tempID
    {
        get { return (int)(this.ViewState["tempID"] ?? 0); }
        set { this.ViewState["tempID"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            connectionString = connectionString_XML;

            if (!IsPostBack)
            {
                LoadCombo();
                LoadData();
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadCombo()
    {
        try
        {
            DataSet ds_Sections = Hipocrate.DB.DBConnection.GetData("select id, name, HiddenColumns from NomenclatorSIUI where Active=1");
            DataView dv = ds_Sections.Tables[0].DefaultView;

            drpNomenclator.DataSource = dv;
            drpNomenclator.DataTextField = "name";
            drpNomenclator.DataValueField = "id";
            drpNomenclator.DataBind();

            this.HiddenColumns = dv[drpNomenclator.SelectedIndex]["HiddenColumns"].ToString().Split(new char[] { ',' });
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void LoadData()
    {
        try
        {
            Hipocrate.DB.DBConnection.variableConnectionString = connectionString;
            DataSet ds_Nomenclator = Hipocrate.DB.DBConnection.GetDataVar("select * from " + drpNomenclator.SelectedItem.Text);

            foreach (DataTable dt in ds_Nomenclator.Tables)
                foreach (DataColumn dc in dt.Columns)
                {
                    dc.ReadOnly = false;
                    dc.AllowDBNull = true;
                }

            SetSession("ds_Nomenclator", ds_Nomenclator);

            DataView dv = ds_Nomenclator.Tables[0].DefaultView;
            grNomenclator.HiddenColumns = this.HiddenColumns;
            grNomenclator.SessionKey = GetSessionKey("ds_Nomenclator");
            grNomenclator.GridViewSortExpression = "";
            grNomenclator.EnableInsert = false;
            grNomenclator.SessionKeyIndex = 0;
            grNomenclator.SessionKeyFilter = dv.RowFilter;
            grNomenclator.SessionKeyColumnHeadersIndex = 1;
            grNomenclator.EnableEdit = false;
            grNomenclator.DataSource = dv;
            grNomenclator.DataBind();

            tempID = dv.Count;
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void ddlNom_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds_Sections = Hipocrate.DB.DBConnection.GetData("select id, name, HiddenColumns from NomenclatorSIUI where Active=1");
        DataView dv = ds_Sections.Tables[0].DefaultView;
        this.HiddenColumns = dv[drpNomenclator.SelectedIndex]["HiddenColumns"].ToString().Split(new char[] { ',' });
        LoadData();
    }

    protected void tabActive_TabChanged(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_Nomenclator = GetSessionDataSet("ds_Nomenclator");
            DataView dv = ds_Nomenclator.Tables[0].DefaultView;
            List<string> arColumns = new List<string>();

            for (int i = 0; i < dv.Table.Columns.Count; ++i)
            {
                if (dv.Table.Columns[i].ColumnName.ToString() != "ID" || dv.Table.Columns[i].ColumnName.ToString() == "IsActive")
                    arColumns.Add(dv.Table.Columns[i].ColumnName.ToString());
            }

            if (ds_Nomenclator != null)
            {
                string columns = arColumns.Join(",");
                string paramList = "@" + arColumns.Join(",@");
                SqlConnection connection = null;
                try
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();

                    for (int i = tempID; i < dv.Count; ++i)
                    {
                        SqlCommand cmd = connection.CreateCommand();
                        cmd.CommandText = "INSERT INTO " + drpNomenclator.SelectedItem.Text +
                         "(" + columns + ") VALUES(" + paramList + ")";
                        foreach (string colName in arColumns)
                            cmd.Parameters.Add(new SqlParameter("@" + colName, dv[i][colName]));
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (SqlException ex)
                {
                    this.SetMessage(ex.Message, true);
                }
                finally
                {
                    if (connection != null)
                        connection.Close();
                }

                this.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
                LoadData();
            }
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }
}
